package com.tourManagement.service.impl;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.BeanListHandler;
import com.tourManagement.entity.Sight;
import com.tourManagement.service.SightService;
import org.springframework.stereotype.Service;
import java.sql.SQLException;
import java.util.List;
@Service
public class SightServiceImpl implements SightService {
    private final String TABLE_NAME = "t_sight";//景点表名
    private final String ID = "sight_id";//景点id的字段名
    @Override
    public List<Sight> getAll() throws SQLException {//获取景点列表
        String sql = "select * from t_sight";
        return Db.use().query(sql,new BeanListHandler<>(Sight.class));//调用hutool工具中的方法实现获取信息
    }

    @Override
    public int update(Sight data) throws SQLException {//更新景点信息
        //调用hutool工具中的方法实现更新信息
        return Db.use().update(Entity.create(TABLE_NAME).parseBean(data,true,true),
                Entity.create(TABLE_NAME).set(ID, data.getSightId()));
    }

    @Override
    public int insert(Sight data) throws SQLException {//添加景点信息
        //调用hutool工具中的方法实现添加信息
        return Db.use().insert(Entity.create(TABLE_NAME).parseBean(data,true,true));
    }

    @Override
    public List<Sight> selectOne(Integer id) throws SQLException {
        String sql = "select * from t_sight where sight_id = ?";
        return Db.use().query(sql, new BeanListHandler<>(Sight.class), id);
    }

    @Override
    public int delete(Integer id) throws SQLException {
        return Db.use().del(Entity.create(TABLE_NAME).set(ID, id));
    }
}
